<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            //inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
            var buildTree = function (inorder, postorder) {
                if (!postorder.length) return null
                let root = {
                    val: postorder[postorder.length - 1],
                    left: null,
                    right: null
                }
                //找出根节点在中序遍历中的下标mid
                let mid = inorder.indexOf(root.val)
                //根据mid 下一次中序[9]  [15,20,7]
                //后序  [9]  [15,7,20]
                root.left = buildTree(inorder.slice(0, mid), postorder.slice(0, mid))
                root.right = buildTree(inorder.slice(mid + 1), postorder.slice(mid, postorder.length - 1))
                return root
            }
        </script>
    </body>
</html>
